﻿using System;
using System.Collections.Generic;
using System.Text;
using com.crawler.common;
using System.Threading;
using System.Text.RegularExpressions;

namespace com.crawler.components
{
    

    public class BookCrawManager
    {
    
        /// <summary>
        /// 站点列表
        /// </summary>
        List<BookSiteInfo> m_list = null;

        /// <summary>
        /// 系统配置信息
        /// </summary>
        private SysConfigInfo m_sys_config = null;

        /// <summary>
        /// 数据处理对象
        /// </summary>
        DBManager m_db_mgr = new DBManager();

        static object m_obj = new object();

        /// <summary>
        /// 运行中的线程集合
        /// </summary>
        Thread[] m_thd_list = null;

        public BookCrawManager()
        {
            try
            {
                m_sys_config = SerializationHelper.Load(typeof(SysConfigInfo), Consts.SysConfigFileName) as SysConfigInfo;

                m_list = SerializationHelper.Load(typeof(List<BookSiteInfo>), Consts.SiteFileName) as List<BookSiteInfo>;

            }
            catch (Exception ex)
            {
                LogHelper.Error("加载系统配置失败" + ex.ToString());
            }
        }

        /// <summary>
        /// 收录新书
        /// </summary>
        public void StartNewBook()
        {
            BaiDuManager t_baidu_mgr = new BaiDuManager();

            if (m_sys_config.CrawlerType == 1) //目录
            {
                t_baidu_mgr.StartTitle();

                m_thd_list = new Thread[m_sys_config.ThreadCount];

                List<SiteUrl> t_list = new List<SiteUrl>();

                foreach (BookSiteInfo t_book_site in m_list)
                {
                    if (t_book_site.IsForbid)
                        continue;

                    for (int t_page_index = t_book_site.StartPageIndex; t_page_index < t_book_site.EndPageIndex; t_page_index++)
                    {

                        string t_book_url = string.Format(t_book_site.BookListUrl, t_page_index);
                        SiteUrl t_site_info = new SiteUrl();
                        t_site_info.Url = t_book_url;
                        t_site_info.SiteId = t_book_site.SiteId;
                        t_site_info.SiteName = t_book_site.SiteName;
                        t_list.Add(t_site_info);

                    }
                }

                int t_per_thd_count = t_list.Count % m_sys_config.ThreadCount == 0 ? t_list.Count / m_sys_config.ThreadCount : t_list.Count / m_sys_config.ThreadCount + 1;

                int t_url_index = 0;

                for (int index = 0; index < m_sys_config.ThreadCount; index++)
                {
                    for (int j = 0; j < t_per_thd_count; j++)
                    {
                        t_url_index = index * t_per_thd_count + j;

                        BookCraw t_craw = new BookCraw();
                        t_craw.m_list = m_list;
                        t_craw.m_sys_config = m_sys_config;
                        m_thd_list[index] = new Thread(new ParameterizedThreadStart(t_craw.CrawlerBookList));

                        if (t_url_index < t_list.Count)
                        {
                            m_thd_list[index].Start(t_list[t_url_index]);
                        }
                    }
                }
            }
            else
            {

                t_baidu_mgr.StartContent();
            }


           
        }

      
    }
}
